﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Linq;

namespace FraudMitigator
{
    public partial class MainScreen : Form
    {
        public static User user;

        public MainScreen(User _user)
        {
            InitializeComponent();
            Strings.InitEditScrnHashtable();
            Strings.InitMainScrnHashtable();
            Strings.InitEditUserFormHashtable();
            Strings.InitPrintLettersFormHashtable();
            mainScreenGridView.DataSource = Queries.mainWindowQuery();
            user = _user;

            // change the column headers to properly capitalized text
            foreach (int pos in Strings.mainScreenColumnNames.Keys)
            {
                mainScreenGridView.Columns[pos].HeaderText = (string)Strings.mainScreenColumnNames[pos];
            }

            HideSensitiveControls(user);
            loggedInStatusLabel.Text = "Logged in as: " + user.UserUsername;
            Strings.Log(user.UserUsername + " logged in.");

            this.Width = mainScreenGridView.Width;
        }

        private void HideSensitiveControls(User user)
        {
            // hide "Users" menuitem if privileges are not high enough
            if (Convert.ToInt32(user.UserAccessLevel) < 3) { usersToolStripMenuItem.Visible = false; }
        }

        private void printLettersMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void addCheckToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AddCheckForm checkForm = new AddCheckForm();
            checkForm.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MainScreen_Shown(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void editToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            EditEntryForm eForm = new EditEntryForm();
            eForm.Show();
        }

        private void addUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddUserForm form = new AddUserForm();
            form.Show();
        }

        private void editUsersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditUsersForm form = new EditUsersForm();
            form.Show();
        }

        private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Strings.Log(user.UserUsername + " logged out.");
            LoginForm form = new LoginForm();
            this.Hide();
            DialogResult result = form.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.Show();
                user = form.user;
                HideSensitiveControls(user);
                loggedInStatusLabel.Text = "Logged in as: " + user.UserUsername;
                Strings.Log(user.UserUsername + " logged in.");
            }
            else { this.Close(); }
        }

        private void printLettersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintLetters printform = new PrintLetters();
            try { printform.Show(); }
            catch { printform.Show(); }
        }

        private void printReportsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Reports rForm = new Reports();
            rForm.Show();
        }

        private void printAllLettersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintLetters l = new PrintLetters();

            l.BatchPrint(3);
            Strings.Log(user.UserUsername + " printed all letters.");
        }

        private void printFirstLettersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintLetters l = new PrintLetters();

            l.BatchPrint(0);
            Strings.Log(user.UserUsername + " printed first letters.");
        }

        private void printSecondLettersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintLetters l = new PrintLetters();

            l.BatchPrint(1);
            Strings.Log(user.UserUsername + " printed second letters.");
        }

        private void printThirdLettersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintLetters l = new PrintLetters();

            l.BatchPrint(2);
            Strings.Log(user.UserUsername + " printed third letters.");
        }
    }
}
